package com.dykj.fireFighting.web.rocketmq.consumer;

import com.dykj.fireFighting.api.base.appNotification.PushModel;
import com.dykj.fireFighting.api.base.push.service.PushService;
import com.dykj.fireFighting.common.core.constant.CommonConstant;
import com.dykj.fireFighting.common.core.constant.WebsocketConstant;
import com.dykj.fireFighting.common.starter.redisson.service.RedisService;
import com.dykj.fireFighting.web.base.websocket.entity.MoreMessage;
import com.dykj.fireFighting.web.model.Device;
import com.dykj.fireFighting.web.rocketmq.message.FirePushMessage;
import com.dykj.fireFighting.web.rocketmq.message.WaterPushMessage;
import com.dykj.fireFighting.web.utils.SpringUtil;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author zhengbo
 */
@RocketMQMessageListener(
        topic = WaterPushMessage.TOPIC,
        consumerGroup = "dy-consumer-group-" + WaterPushMessage.TOPIC
)
public class WaterPushConsumer  implements RocketMQListener<WaterPushMessage> {
    public static final Logger LOGGER = LoggerFactory.getLogger(WaterPushConsumer.class);
    private static final RedisService redisService= SpringUtil.getBean(RedisService.class);
    private static PushService pushService=(PushService)SpringUtil.getBean("pushService");
    @Override
    public void onMessage(WaterPushMessage waterPushMessage) {
        Device device = waterPushMessage.getDevice();
        String time = waterPushMessage.getAlarmTime().substring(11);
        MoreMessage moreMessage=new MoreMessage();
        moreMessage.setMsgId(waterPushMessage.getDevHisId());
        PushModel pushModel=new PushModel();
        if("液位低限报警".equals(device.getStatus())){
            pushModel.setTitle("液位低限报警通知");
            pushModel.setBody(device.getName()+"设备于今天"+time+"上报压力为："+device.getCalculationValue()+",低于标定量程："+device.getThresholdL()+";请及时处理");
            pushToApp(waterPushMessage, pushModel);

            moreMessage.setMsgTitle("液位低限报警通知");
            moreMessage.setCmd(WebsocketConstant.CMD_WATER);
            moreMessage.setMsgTxt(device.getName()+"设备于今天"+time+"上报液位为："+device.getCalculationValue()+",低于标定量程："+device.getThresholdL()+";请及时处理");
            moreMessage.setUserIds(waterPushMessage.getUnit().getUnitUserIds().toArray(new String[0]));
            redisService.convertAndSend("channel:moreMessage",moreMessage);
            moreMessage.setMsgTxt(waterPushMessage.getUnit().getUnitName()+"的"+device.getName()+"设备于今天"+time+"上报液位为："+device.getCalculationValue()+",低于标定量程："+device.getThresholdL()+";请及时处理");
            moreMessage.setUserIds(waterPushMessage.getUnit().getCenterUserIds().toArray(new String[0]));
            redisService.convertAndSend("channel:moreMessage",moreMessage);
        }
        if("压力低限报警".equals(device.getStatus())){
            pushModel.setTitle("压力低限报警通知");
            pushModel.setBody(device.getName()+"设备于今天"+time+"上报压力为："+device.getCalculationValue()+",低于标定量程："+device.getThresholdL()+";请及时处理");
            pushToApp(waterPushMessage, pushModel);

            moreMessage.setMsgTitle("压力低限报警通知");
            moreMessage.setCmd(WebsocketConstant.CMD_WATER);
            moreMessage.setMsgTxt(device.getName()+"设备于今天"+time+"上报压力为："+device.getCalculationValue()+",低于标定量程："+device.getThresholdL()+";请及时处理");
            moreMessage.setUserIds(waterPushMessage.getUnit().getUnitUserIds().toArray(new String[0]));
            redisService.convertAndSend("channel:moreMessage",moreMessage);
            moreMessage.setMsgTxt(waterPushMessage.getUnit().getUnitName()+"的"+device.getName()+"设备于今天"+time+"上报压力位为："+device.getCalculationValue()+",低于标定量程："+device.getThresholdL()+";请及时处理");
            moreMessage.setUserIds(waterPushMessage.getUnit().getCenterUserIds().toArray(new String[0]));
            redisService.convertAndSend("channel:moreMessage",moreMessage);
        }
        if("液位高限报警".equals(device.getStatus())){
            pushModel.setTitle("液位高限报警通知");
            pushModel.setBody(device.getName()+"设备于今天"+time+"上报液位为："+device.getCalculationValue()+",高于标定量程："+device.getThresholdH()+";请及时处理");
            pushToApp(waterPushMessage, pushModel);

            moreMessage.setMsgTitle("液位高限报警通知");
            moreMessage.setCmd(WebsocketConstant.CMD_WATER);
            moreMessage.setMsgTxt(device.getName()+"设备于今天"+time+"上报液位为："+device.getCalculationValue()+",高于标定量程："+device.getThresholdH()+";请及时处理");
            moreMessage.setUserIds(waterPushMessage.getUnit().getUnitUserIds().toArray(new String[0]));
            redisService.convertAndSend("channel:moreMessage",moreMessage);
            moreMessage.setMsgTxt(waterPushMessage.getUnit().getUnitName()+"的"+device.getName()+"设备于今天"+time+"上报液位为："+device.getCalculationValue()+",高于标定量程："+device.getThresholdH()+";请及时处理");
            moreMessage.setUserIds(waterPushMessage.getUnit().getCenterUserIds().toArray(new String[0]));
            redisService.convertAndSend("channel:moreMessage",moreMessage);
        }
        if("压力高限报警".equals(device.getStatus())){
            pushModel.setTitle("液位低限报警通知");
            pushModel.setBody(device.getName()+"设备于今天"+time+"上报压力为："+device.getCalculationValue()+",高于标定量程："+device.getThresholdH()+";请及时处理");
            pushModel.setAlias(waterPushMessage.getUnit().getUnitId());
            pushToApp(waterPushMessage, pushModel);

            moreMessage.setMsgTitle("压力高限报警通知");
            moreMessage.setCmd(WebsocketConstant.CMD_WATER);
            moreMessage.setMsgTxt(device.getName()+"设备于今天"+time+"上报压力为："+device.getCalculationValue()+",高于标定量程："+device.getThresholdH()+";请及时处理");
            moreMessage.setUserIds(waterPushMessage.getUnit().getUnitUserIds().toArray(new String[0]));
            redisService.convertAndSend("channel:moreMessage",moreMessage);
            moreMessage.setMsgTxt(waterPushMessage.getUnit().getUnitName()+"的"+device.getName()+"设备于今天"+time+"上报压力位为："+device.getCalculationValue()+",高于标定量程："+device.getThresholdH()+";请及时处理");
            moreMessage.setUserIds(waterPushMessage.getUnit().getCenterUserIds().toArray(new String[0]));
            redisService.convertAndSend("channel:moreMessage",moreMessage);
        }
        if("液位恢复".equals(device.getStatus())){
            pushModel.setTitle("液位低限报警通知");
            pushModel.setBody(device.getName()+"设备于今天"+time+"液位已恢复;当前液位为："+device.getCalculationValue());
            pushToApp(waterPushMessage, pushModel);

            moreMessage.setMsgTitle("液位恢复通知");
            moreMessage.setCmd(WebsocketConstant.CMD_WATER);
            moreMessage.setMsgTxt(device.getName()+"设备于今天"+time+"液位已恢复;当前液位为："+device.getCalculationValue());
            moreMessage.setUserIds(waterPushMessage.getUnit().getUnitUserIds().toArray(new String[0]));
            redisService.convertAndSend("channel:moreMessage",moreMessage);
            moreMessage.setMsgTxt(waterPushMessage.getUnit().getUnitName()+"的"+device.getName()+"设备于今天"+time+"液位已恢复;当前液位为："+device.getCalculationValue());
            moreMessage.setUserIds(waterPushMessage.getUnit().getCenterUserIds().toArray(new String[0]));
            redisService.convertAndSend("channel:moreMessage",moreMessage);
        }
        if("压力恢复".equals(device.getStatus())){
            pushModel.setTitle("液位低限报警通知");
            pushModel.setBody(device.getName()+"设备于今天"+time+"压力已恢复;当前液位为："+device.getCalculationValue());
            pushToApp(waterPushMessage, pushModel);

            moreMessage.setMsgTitle("压力恢复通知");
            moreMessage.setCmd(WebsocketConstant.CMD_WATER);
            moreMessage.setMsgTxt(device.getName()+"设备于今天"+time+"压力已恢复;当前液位为："+device.getCalculationValue());
            moreMessage.setUserIds(waterPushMessage.getUnit().getUnitUserIds().toArray(new String[0]));
            redisService.convertAndSend("channel:moreMessage",moreMessage);
            moreMessage.setMsgTxt(waterPushMessage.getUnit().getUnitName()+"的"+device.getName()+"设备于今天"+time+"压力已恢复;当前液位为："+device.getCalculationValue());
            moreMessage.setUserIds(waterPushMessage.getUnit().getCenterUserIds().toArray(new String[0]));
            redisService.convertAndSend("channel:moreMessage",moreMessage);
        }
    }

    private void pushToApp(WaterPushMessage waterPushMessage, PushModel pushModel) {
        pushModel.setAlias(waterPushMessage.getUnit().getUnitId());
        pushModel.setClickType(CommonConstant.APPCLICKTYPE);
        boolean b = pushService.pushAlias(pushModel);
        System.out.println("===============================================================app推送结果："+b);
    }
}
